<template>
  <div>
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0px">
      <el-button
        type="primary"
        icon="Plus"
        :disabled="categoryStore.categoryInfo.categoryId3 ? false : true"
      >
        添加SPU
      </el-button>
      <!-- 展示已有的spu数据 -->
      <el-table style="margin: 10px 0px" border :data="records">
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="80"
        ></el-table-column>
        <el-table-column label="SPU名称" prop="spuName"></el-table-column>
        <el-table-column label="SPU描述" prop="description"></el-table-column>
        <el-table-column label="SPU操作">
          <template #default="{ row, $index }">
            <el-button
              type="primary"
              size="small"
              icon="Plus"
              title="添加SPU列表"
            ></el-button>
            <el-button
              type="warning"
              size="small"
              icon="Edit"
              title="修改SPU"
            />
            <el-button
              type="info"
              size="small"
              icon="View"
              title="查看SKU列表"
            />
            <el-button
              type="danger"
              size="small"
              icon="Delete"
              title="删除SKU列表"
            />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[3, 5, 7, 9]"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
        @current-change="getHasSPU"
        @size-change="changeSize"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import type { HasSpuResponseData, Records } from '@/services/product/spu/type'
import { ref, watch } from 'vue'
import { reqHasSpu } from '@/services/product/spu'
// 引入分类仓库
import useCategoryStore from '@/store/modules/category'
let categoryStore = useCategoryStore()
// 场景数据
let scene = ref<number>(0)
// 分页器默认页码
let pageNo = ref<number>(1)
// 每页展示数据条数
let pageSize = ref<number>(3)

// 监听三级分类ID变化
watch(
  () => categoryStore.categoryInfo.categoryId3,
  () => {
    if (!categoryStore.categoryInfo.categoryId3) return
    getHasSPU()
  },
)
// 获取某一个三级分类下全部已有的SPU
let records = ref<Records>([])
let total = ref<number>(0) // 存储已有的spu
const getHasSPU = async () => {
  const res: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.categoryInfo.categoryId3,
  )
  if (res.code == 200) {
    records.value = res.data.records
    total.value = res.data.total
  }
}

// 分页器的下拉菜单发生变化触发
const changeSize = () => {
  getHasSPU()
}
</script>

<style lang="scss" scoped></style>
